from sqlalchemy.ext.asyncio import AsyncSession
from app.models.testModel import Test
from sqlalchemy.future import select
from app.schemas.testSchema import TestCreate
from typing import List

async def get_test(db: AsyncSession, test_id: int):
    return await db.get(Test, test_id)

async def get_tests(db: AsyncSession, skip: int = 0, limit: int = 100):
    result = await db.execute(select(Test).offset(skip).limit(limit))
    return result.scalars().all()

async def create_test(db: AsyncSession, test: TestCreate):
    db_test = Test(**test.dict())
    db.add(db_test)
    await db.commit()
    await db.refresh(db_test)
    return db_test

async def update_test(db: AsyncSession, test_id: int, test: TestCreate):
    db_test = await get_test(db, test_id)
    if db_test:
        for field, value in test.dict().items():
            setattr(db_test, field, value)
        await db.commit()
        await db.refresh(db_test)
    return db_test

async def delete_test(db: AsyncSession, test_id: int):
    db_test = await get_test(db, test_id)
    if db_test:
        await db.delete(db_test)
        await db.commit()
    return db_test